package com.wym.exampleproject.distributedzookeeperlock.service.impl;

import com.wym.exampleproject.distributedzookeeperlock.service.IDistributedZkLockService;
import com.wym.exampleproject.distributedzookeeperlock.service.ILockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class LockServiceImpl  implements ILockService {

    @Autowired
    private IDistributedZkLockService distributedZkLockService;

    public static String key = "asd";
    public static int timeout = 10000000;


    @Override
    public String one() {
        try {
            if (distributedZkLockService.lock()) {
                log.info("one接口 获取锁");
                Thread.sleep(8000);
            }else{
                log.info("one接口 获取锁失败");
            }
        } catch (InterruptedException e) {
            distributedZkLockService.unLock();
            e.printStackTrace();
        } finally {
            distributedZkLockService.unLock();

        }
        return "方法一返回结果"+System.currentTimeMillis();
    }

    @Override
    public String two() {
        try {
            if (distributedZkLockService.lock()) {
                log.info("two接口 获取锁");
//                Thread.sleep(8000);
            }else{
                log.info("two接口 获取锁失败");
            }
        } finally {
            distributedZkLockService.unLock();
        }
        return "方法二返回结果"+System.currentTimeMillis();

    }
}
